package com.atguigu.dga.governance.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.governance.bean.GovernanceAssessGlobal;
import com.atguigu.dga.governance.bean.GovernanceAssessTecOwner;
import com.atguigu.dga.governance.service.GovernanceAssessDetailService;
import com.atguigu.dga.governance.service.GovernanceAssessGlobalService;
import com.atguigu.dga.governance.service.GovernanceAssessTecOwnerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 治理考评结果接口 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2023-11-25
 */
@RestController
@RequestMapping("/governance")
public class GovernanceAssessController {


    @Autowired
    GovernanceAssessGlobalService governanceAssessGlobalService;


    @Autowired
    GovernanceAssessTecOwnerService governanceAssessTecOwnerService;

    @Autowired
    GovernanceAssessDetailService governanceAssessDetailService;

    ///governance/globalScore
    //{  "assessDate":"2023-04-01" ,"sumScore":90, "scoreList":[20,40,34,55,66] }
    @GetMapping("/globalScore")
    public String getGlobalScore(){

        QueryWrapper<GovernanceAssessGlobal> queryWrapper = new QueryWrapper<GovernanceAssessGlobal>().inSql("assess_date", "select max(assess_date) from  governance_assess_global");
        GovernanceAssessGlobal governanceAssessGlobal = governanceAssessGlobalService.getOne(queryWrapper);

        JSONObject resultJsonObj = new JSONObject();
        resultJsonObj.put("assessDate",governanceAssessGlobal.getAssessDate());
        resultJsonObj.put("sumScore",governanceAssessGlobal.getScore());

        List<BigDecimal> scoreList=new ArrayList<>();
        scoreList.add(governanceAssessGlobal.getScoreSpec());
        scoreList.add(governanceAssessGlobal.getScoreStorage());
        scoreList.add(governanceAssessGlobal.getScoreCalc());
        scoreList.add(governanceAssessGlobal.getScoreQuality());
        scoreList.add(governanceAssessGlobal.getScoreSecurity());
        resultJsonObj.put("scoreList",scoreList);


        return resultJsonObj.toJSONString();

    }

    @GetMapping("/rankList")
    public String getRankList(){
        QueryWrapper<GovernanceAssessTecOwner> queryWrapper = new QueryWrapper<GovernanceAssessTecOwner>()
                .inSql("assess_date", "select max(assess_date) from  governance_assess_tec_owner")
                .orderByDesc("score")
                .select("tec_owner as tecOwner","score");

        List<Map<String, Object>> rankList = governanceAssessTecOwnerService.listMaps(queryWrapper);

        return  JSON.toJSONString(rankList);

    }


    @GetMapping("/problemNum")
    public String  getProblemNum(){
        QueryWrapper<GovernanceAssessDetail> queryWrapper = new QueryWrapper<GovernanceAssessDetail>()
                .inSql("assess_date", "select max(assess_date) from  governance_assess_detail")
                .select("sum(if(governance_type='SPEC' and  assess_score<10,1,0))  SPEC"
                        , "sum(if(governance_type='STORAGE' and  assess_score<10,1,0))  STORAGE"
                        , "sum(if(governance_type='CALC' and  assess_score<10,1,0))  CALC "
                        , "sum(if(governance_type='QUALITY' and  assess_score<10,1,0)) QUALITY"
                        , "sum(if(governance_type='SECURITY' and  assess_score<10,1,0)) SECURITY"
                );

        Map<String, Object> map = governanceAssessDetailService.getMap(queryWrapper);

        return JSON.toJSONString(map);
    }

    @GetMapping("/problemList/{governType}/{pageNo}/{pageSize}")
    public  String getProblemList(@PathVariable("governType")String governType ,@PathVariable("pageNo") Integer pageNo,@PathVariable("pageSize") Integer pageSize ){
        int rowNo=  (pageNo-1)*pageSize;

        QueryWrapper<GovernanceAssessDetail> queryWrapper = new QueryWrapper<GovernanceAssessDetail>()
                .inSql("assess_date", "select max(assess_date) from  governance_assess_detail")
                .eq("governance_type",governType)
                .lt("assess_score",10)
                .last("limit "+rowNo+","+pageSize );

        List<GovernanceAssessDetail> governanceAssessDetailList = governanceAssessDetailService.list(queryWrapper);

        return  JSON.toJSONString(governanceAssessDetailList);

    }

}
